class cloud{
    constructor(options) {
        options = options || {};
        for(let key in options.cloud){
            this[key] = options.cloud[key];
        }
        this.parent = options.that;
        this.ajax = this._ajax;
    }


     // 同意处理页面的请求
    async _ajax(options = {}, serviceType = this.parent.config.serviceType, errCatch = this.parent.config.errCatch) {
        let { path = '', method = 'GET', params = {}, body = {}, headers = {}, exts = {}, name = '', data = {}, handler = 'main' } = options;
        return new Promise(async (resolve, reject) => {
            if (serviceType === 'func') {
                if (!name) {
                    reject('name need!');
                }
                try {
                    const res = await this.function.invoke(name, data, handler, exts);
                    resolve(res);
                } catch (err) {
                    console.log(err);
                    if (errCatch) {
                        console.log('跳转到异常页面');
                        this.parent.launchErrPage(404);
                    } else {
                        reject(err);
                    }
                }
            } else if (serviceType === 'app') {
                if (!path) {
                    reject('path need!');
                }
                try {
                    if (method === 'POST' && headers === {}) {
                        headers = { 'content-type': "application/json" };
                    }
                    const res = await this.application.httpRequest({
                        path,
                        method,
                        headers,
                        params,
                        body,
                        exts,
                    });
                    resolve(res);
                } catch (err) {
                    if (errCatch) {
                        console.log('跳转到异常页面');
                        this.parent.launchErrPage(404);
                    } else {
                        reject(err);
                    }
                }
            } else {
                reject('cloud service abnormal');
            }
        });
    }
}
export default cloud;